package com.haierp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapper.WarehouseMapper;
import com.haierp.model.Warehouse;
import com.haierp.service.IWarehouseService;
import com.wangzhixuan.commons.shiro.ShiroUser;

@Service
public class WarehouseServiceImpl extends SuperServiceImpl<WarehouseMapper, Warehouse>  implements IWarehouseService {
	private Map<Long, List<Warehouse>> warehouseListCompanyMap = new HashMap<Long, List<Warehouse>>();
	
	private Map<Long, Map<Long, String>> warehouseCompanyMap = new HashMap<Long, Map<Long, String>>();
	
	private Map<Long, Map<Long, Integer>> warehouseSeqComapnyMap = new HashMap<Long, Map<Long, Integer>>();
	
	@Autowired
	private WarehouseMapper warehouseMapper;

	@Override
	public void addWarehouse(Warehouse warehouse) {
		warehouse.setGmtCreate(new Date());
		warehouse.setGmtModify(new Date());
		warehouseMapper.insert(warehouse);
	}

	@Override
	public void updateWarehouse(Warehouse warehouse) {
		warehouse.setGmtModify(new Date());
		warehouseMapper.updateSelectiveById(warehouse);
	}

	@Override
	public List<Warehouse>  queryWarehouses() {
		Long companyId = 1L;
		if(this.getShiroUser() == null){
		     companyId = 1L;
		}else{
			 companyId = this.getShiroUser().getCompanyId();
		}
		List<Warehouse> warehouseList = warehouseListCompanyMap.get(companyId);
		if(warehouseList!=null && warehouseList.size()>0) {
			return warehouseList;
		} else {
			this.initWarehouseList();
			return warehouseListCompanyMap.get(companyId);
		}
	}
	
	public void initWarehouseList() {
		Long companyId = 1L;
		if(this.getShiroUser() == null){
		   companyId = 1L;
		}else{
			companyId = this.getShiroUser().getCompanyId();
		}
		EntityWrapper<Warehouse> wrapper = new EntityWrapper<Warehouse>();
		wrapper.orderBy("id");
		List<Warehouse> warehouseList = warehouseMapper.selectList(wrapper);
		warehouseListCompanyMap.put(companyId, warehouseList);
		
		Map<Long, String> warehouseMap = new HashMap<Long, String>();
		Map<Long, Integer> warehouseSeqMap = new HashMap<Long, Integer>();
		
		warehouseList.forEach(warehouse->{
			warehouseMap.put(warehouse.getId(), warehouse.getName());
			warehouseSeqMap.put(warehouse.getId(), warehouse.getSeq());
		});
		warehouseCompanyMap.put(companyId, warehouseMap);
		warehouseSeqComapnyMap.put(companyId, warehouseSeqMap);
	}
	
	@Override
	public Map<Long, String> getWarehouseMap() {
		Long companyId = 1L;
		if(this.getShiroUser() == null){
		     companyId = 1L;
		}else{
			 companyId = this.getShiroUser().getCompanyId();
		}
		Map<Long, String> warehouseMap = warehouseCompanyMap.get(companyId);
		if(warehouseMap!=null && warehouseMap.size()>0) {
			return warehouseMap;
		} else {
			this.initWarehouseList();
			return warehouseCompanyMap.get(companyId);
		}
	}
	@Override
	public Map<Long, Integer> getWarehouseSeqMap() {
		Long companyId = 1L;
		if(this.getShiroUser() == null){
		     companyId = 1L;
		}else{
			 companyId = this.getShiroUser().getCompanyId();
		}
		Map<Long, Integer> warehouseSeqMap = warehouseSeqComapnyMap.get(companyId);
		if(warehouseSeqMap!=null && warehouseSeqMap.size()>0) {
			return warehouseSeqMap;
		} else {
			this.initWarehouseList();
			return warehouseSeqComapnyMap.get(companyId);
		}
	}

	@Override
	public String getWarehouseById(Long id) {
		Map<Long, String> warehouseMap = this.getWarehouseMap();
		return warehouseMap.get(id);
	}
	
	 /**
     * 获取当前登录用户对象
     * @return {ShiroUser}
     */
    public ShiroUser getShiroUser() {
        return (ShiroUser) SecurityUtils.getSubject().getPrincipal();
    }
	
}
